package com.kyub.test.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.validation.BindingResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.text.SimpleDateFormat;

import com.kyub.test.model.Pet;
import com.kyub.test.model.MockPersonDao;
import com.kyub.test.model.Person;
import com.kyub.spring.gears.model.PojoRegistry;
import com.kyub.spring.gears.model.PojoDescriptor;
import com.kyub.spring.gears.introspect.PojoDescriptorFactory;
import com.kyub.spring.gears.web.PropertyEditorBuilder;

import javax.naming.ConfigurationException;

/**
 * Created by IntelliJ IDEA.
 * User: sreghenzi
 * Date: 17-mar-2009
 * Time: 16.12.09
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class PetController {
    private static final ArrayList volatiles = new ArrayList();
    private static final String COMMAND_POJO_NAME = "commandPojo";

    @Autowired
    private PojoRegistry pojoRegistry;
    private PojoDescriptorFactory factory;
    private PojoDescriptor petPojoDescriptor;
    private static final List owners = new MockPersonDao().list();

    @RequestMapping(value = "/pet/contestualAdd")
    public ModelAndView contestualAdd(@ModelAttribute(COMMAND_POJO_NAME)Pet pojoInstance, BindingResult bindingResult){


        ModelAndView model = new ModelAndView();
        checkPD();
        model.addObject("pojoDescriptor", petPojoDescriptor);
        model.addObject(COMMAND_POJO_NAME, new Pet());
        if (bindingResult.hasErrors()) {
            model.addObject(COMMAND_POJO_NAME, pojoInstance);
        }
        else if(pojoInstance != null){
            volatiles.add(pojoInstance);
        }
        model.addObject("gears_restfull_prefix","clinic");
        model.setViewName("custom/composed");
        model.addObject("pojoList",volatiles);
        final HashMap hashMap = new HashMap();
        hashMap.put("owner", owners);
        model.addObject("related", hashMap);
        return model;

    }

     @InitBinder
    public void initBinder(WebDataBinder binder) {
         final PropertyEditorBuilder pb = new PropertyEditorBuilder();
         SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
         checkPD();
         try {
            //TODO atowiringbean
            binder.registerCustomEditor(Person.class, pb.buildPropertyEditorSupport(factory.buildDescriptorByClass(Person.class),new MockPersonDao()));
         } catch (ConfigurationException e) {
             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
         }

     }

    private void checkPD() {
        if(petPojoDescriptor == null){
        factory = new PojoDescriptorFactory(pojoRegistry);
        try {
            petPojoDescriptor = factory.buildDescriptorByClass(Pet.class);

        } catch (ConfigurationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        }
    }

}
